<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>

    // https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash






















    
    /*
      protocol协议
        http: 超文本传输协议
        https：加密的超文本传输协议
        ws：websocket（前后端实时通信）协议
        wss：加密的websocket协议
        file：本地文件协议


      auth权限
        user:pass
        一般可以不用


      hostname 主机名，域名
        sub.example.com
    
    
      port 端口号
        :8080
        http协议的默认端口号是80
        https的默认端口号是443


      pathname 路径名
        /p/a/t/h  一般用于做页面跳转



      search 查询字符串
        ?id=763264324257&ns=1&priceTId=2147824117268163596877739ebff4&skuId=5297743461810&spm=a21n57.1.item.14.2cff523cxDjPMW&utparam=%7B"aplus_abtest"%3A"689017c021b95df2293942dd9c85be30"%7D&xxc=ad_ztc
        一般用来做页面传参


      hash 哈希值
        #hash
        页面内的跳转，也可以用来做hash路由
    */ 

    const url = "https://user:pass@sub.example.com:8080/p/a/t/h?id=263264324257&ns=1&priceTId=2147824117268163596877739ebff4&skuId=5297743461810&spm=a21n57.1.item.14.2cff523cxDjPMW&xxc=ad_ztc"

    // 取2147824117268163596877739ebff4

    const getPriceTId = (url, str) => {
      const query = url.split("?")[1]; // 用split方法做分割，只需要第二项
      // console.log(query); // id=263264324257&ns=1&priceTId=2147824117268163596877739ebff4
      const queryArr = query.split("&");
      // console.log(queryArr); // ["id=263264324257", "ns=1", "priceTId=2147824117268163596877739ebff4"]
      const queryArr2 = queryArr.map(item => {
        return item.split("=");
      }) // [["id", "263264324257"], ["ns", 1], ["priceTId", "2147824117268163596877739ebff4"]]
      // console.log(queryArr2);
      const obj = queryArr2.reduce((obj, item) => {
        obj[item[0]] = item[1]
        return obj;
      }, {})
      // console.log(obj); // {id: "263264324257", ns: "1", priceTId: "2147824117268163596877739ebff4"}
      console.log(obj[str]);
      
    };
    getPriceTId(url, "priceTId")
    getPriceTId(url, "ns")



    const url2 = "https://nodejs.org/docs/latest/api/url.html?abc=123&name=zhangsan"

    getPriceTId(url2, "name")
  </script>
</body>
</html>